package org.cupk2.service.controller;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.cupk2.files.service.ResourceFileServiceInter;
import org.cupk2.service.inter.WebResourceServiceInter;
import org.cupk2.zhzm.service.ZhzmKnowServiceInter;
import org.cupk2.tools.web.Cupk2HttpUtils;
import org.cupk2.tools.web.CupkResponseCode;
import org.cupk2.tools.web.CupkResponseResult;
import org.cupk2.tools.web.domain.WebDoc;
import org.cupk2.tools.web.dto.UrlDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 知识
 *
 * @author cbtg自动生成  2025-2-4 12:13:51
 */
@RestController
@RequestMapping("/api/webpage")
@Slf4j
public class WebDownloadWebPageController {
    @Autowired
    private ZhzmKnowServiceInter zhzmKnowServiceImpl;

    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;

    @Autowired
    private WebResourceServiceInter webResourceService;
    /**
     * 下载web页面
     *
     * @param url
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmKnow.add')")
    @PostMapping
    public CupkResponseResult addSubmit(@Valid @RequestBody UrlDto url) {
        if (Cupk2HttpUtils.isHasIpOrPort(url.getUrl()) || !Cupk2HttpUtils.isUrl(url.getUrl())) {
            return CupkResponseResult.getInstance(CupkResponseCode.ERROR, "请检查url格式", null);
        }
        WebDoc doc = webResourceService.downloadWebPage(url.getUrl());
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, doc);
    }
}
